package com.example.zhinongbao.activity;
// 地址管理



import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import com.example.zhinongbao.R;
import com.example.zhinongbao.adapter.AddressAdapter;
import com.example.zhinongbao.model.Address;
import java.util.ArrayList;
import java.util.List;

public class AddressManagementActivity extends AppCompatActivity
        implements AddressAdapter.OnAddressClickListener {

    private RecyclerView recyclerView;
    private AddressAdapter adapter;
    private LinearLayout emptyView;
    private Button btnNewAddress;

    private List<Address> addressList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_management);

        // 初始化视图
        recyclerView = findViewById(R.id.recycler_view_addresses);
        emptyView = findViewById(R.id.empty_view);
        btnNewAddress = findViewById(R.id.btn_new_address);

        // 设置RecyclerView
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AddressAdapter(addressList, this);
        recyclerView.setAdapter(adapter);

        // 设置按钮点击事件
        btnNewAddress.setOnClickListener(v -> {
            // 启动新建地址Activity
            AddAddressActivity.start(AddressManagementActivity.this);
        });

        // 返回按钮
        findViewById(R.id.btn_back).setOnClickListener(v -> finish());

        // 加载地址数据
        loadAddresses();
    }

    private void loadAddresses() {
        // 临时测试数据
        addressList.clear();
        addressList.add(new Address(1, "张三", "13800138000", "北京市朝阳区", "某某街道100号", true));
        addressList.add(new Address(2, "李四", "13900139000", "上海市浦东新区", "科技园88号", false));

        // 更新UI
        if (addressList.isEmpty()) {
            emptyView.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            emptyView.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
            adapter.notifyDataSetChanged();
        }
    }

    // 实现点击事件接口
    @Override
    public void onAddressClick(Address address) {
        // 处理地址点击
    }

    @Override
    public void onEditClick(Address address) {
        // 编辑地址
        AddAddressActivity.start(AddressManagementActivity.this, address);
    }

    @Override
    public void onDeleteClick(Address address) {
        // 删除地址
        addressList.remove(address);
        adapter.notifyDataSetChanged();
        checkEmptyView();
    }

    @Override
    public void onSetDefault(Address address) {
        // 设置为默认地址
        for (Address addr : addressList) {
            addr.setDefault(addr.getId() == address.getId());
        }
        adapter.notifyDataSetChanged();
    }

    private void checkEmptyView() {
        if (addressList.isEmpty()) {
            emptyView.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 重新加载数据（当从添加/编辑页面返回时）
        loadAddresses();
    }
}